﻿
//收付款记录明细操作
function addNewRow() {
    var tempId = $("#hidbillDetailId").val();
    var billId = $("#Id").val();
    var row = manager.getSelectedRow();
    //参数1:rowdata(非必填)
    //参数2:插入的位置 Row Data 
    //参数3:之前或者之后(非必填)
    manager.addRow({
        ID: tempId,
        ProductReturnBillID: billId,
        StockID: '',
        ReturnAmount: 0,
        ReturnPrice: 0,
        DeleteState: '正常',
        Remarks:''
    }, row, document.getElementById("chkbefore"));
} 

var columnsSK =
            [
                { display: 'Key', name: 'ID', type: 'text', hide: 1 },
                { display: '收款金额(元)', name: 'ReceivedMoney', mintWidth: 80, width: 120 }, //, type: 'int', mintWidth: 40, width: 100
                {display: '收款优惠(元)', name: 'ReceivedMoneyPft', mintWidth: 80, width: 120 },
                //{display: '本次付款金额', name: 'PaidMoney', mintWidth: 100, width: 140, type: 'selfDate' },
              //  { display: '付款优惠', name: 'OmitMoney', mintWidth: 100, width: 140 },
               // { display: '票据方式', name: 'InvoiceTypeName', mintWidth: 80, width: 140 },
              //  { display: '多开税金', name: 'TaxMoney', mintWidth: 80, width: 120 },
              //  { display: '多开发票金额', name: 'MoreTaxMoney', mintWidth: 150, width: 200 },
              //  { display: '发票号码', name: 'InvoiceNO', mintWidth: 150, width: 200 },
                { display: '支付方式', name: 'PayTypeName', mintWidth: 150, width: 200 },
               // { display: '单子状态', name: 'SettleState', mintWidth: 150, width: 200 },
               // { display: '应付优惠', name: 'BillPayPft', mintWidth: 150, width: 200 },
               // { display: '删除状态', name: 'DeleteState', mintWidth: 80, width: 140 },
               // { display: '结算状态', name: 'SettleState', mintWidth: 80, width: 120 }
                 {display: '备注', name: 'Remarks', mintWidth: 150, width: 200 }
             ];
var columnsFK =
            [
                { display: 'Key', name: 'ID', type: 'text', hide: 1 },
               // { display: '本次收款金额', name: 'ReceivedMoney', mintWidth: 80, width: 120 }, //, type: 'int', mintWidth: 40, width: 100
                {display: '本次付款金额(元)', name: 'PaidMoney', mintWidth: 100, width: 140, type: 'selfDate' },
                { display: '付款优惠(元)', name: 'OmitMoney', mintWidth: 100, width: 140 },
                //{ display: '票据方式', name: 'InvoiceTypeName', mintWidth: 80, width: 140 },
               // { display: '多开税金', name: 'TaxMoney', mintWidth: 80, width: 120 },
               // { display: '多开发票金额', name: 'MoreTaxMoney', mintWidth: 150, width: 200 },
             //   { display: '发票号码', name: 'InvoiceNO', mintWidth: 150, width: 200 },
                { display: '支付方式', name: 'PayTypeName', mintWidth: 150, width: 200 },
           //     { display: '单子状态', name: 'SettleState', mintWidth: 150, width: 200 },
                {display: '应付优惠(元)', name: 'BillPayPft', mintWidth: 150, width: 200 },
//                { display: '删除状态', name: 'DeleteState', mintWidth: 80, width: 140 },
               // {display: '结算状态', name: 'SettleState', mintWidth: 80, width: 120 },
                 { display: '备注', name: 'Remarks', mintWidth: 150, width: 200 }
             ];
var manager;
//init controls
$(document).ready(function () {
    initligerGrid();
});

function initligerGrid() {
    manager = null;
    manager = $("#ReceivableItemsGrid").ligerGrid({
        columns: $("#SFBillType").val() == "0" ? columnsSK : columnsFK,
        //  title: '收付款记录明细',//不能要标题，修改的BUG，不在ligerui.min.js文件中，所以这个BUG还是存在
        showTitle: false,
        width: '100%',
        enabledSort: true,
        enabledEdit: true,
        height: '99%',
        checkbox: false,
        rownumbers: true,
        toolbar: { items: [
                                    { text: '历史收款记录' },
                                    { text: '删除', click: deleteOperation, img: '../Content/images/delete.gif' }
                                    ]
        },
        isChecked: f_isChecked, onCheckRow: f_onCheckRow, onCheckAllRow: f_onCheckAllRow,
        url: "../ReceivablePayable/GetJsonOfBillItems?billId=" + $("#ReceivablePayableBillID").val() + "&rand=" + Math.random()
    });
}

//delete
function deleteOperation() {
    var row = manager.getSelectedRow();
    var ids = checkedCustomer.join(',');
    var detailId = (row == undefined || row == null) ? null : row["ID"];
    var billId = $("#ReceivablePayableBillID").val();
    if (row != null && row != undefined && detailId != null && $.trim(detailId.toString()).length > 0) {
        ids = detailId.toString() + ",";
        $.ligerDialog.confirm('确定要删除吗？', function (yes) {
            if (yes) {
                if (billId.length > 0) {
                    manager.deleteSelectedRow();
                    $.get("../ReceivablePayable/Delete?billId=" + billId + "&ids=" + ids, {}, function (data) {
                        if (data != undefined && data == "9") {
                            ajaxSubmitSuccess(); //公共类：提交成功后关闭当前窗口
                        }
                    });

                }

            }
        });
    }
    else {
        if (!row) { $.ligerDialog.success('请选择需要删除行！'); return; }
    }
}




function f_onCheckAllRow(checked) {
    for (var rowid in this.records) {
        if (checked)
            addCheckedCustomer(this.records[rowid]['CustomerID']);
        else
            removeCheckedCustomer(this.records[rowid]['CustomerID']);
    }
}

/*
该例子实现 表单分页多选
即利用onCheckRow将选中的行记忆下来，并利用isChecked将记忆下来的行初始化选中
*/

var checkedCustomer = [];
function findCheckedCustomer(CustomerID) {
    for (var i = 0; i < checkedCustomer.length; i++) {
        if (checkedCustomer[i] == CustomerID) return i;
    }
    return -1;
}
function addCheckedCustomer(CustomerID) {
    if (findCheckedCustomer(CustomerID) == -1)
        checkedCustomer.push(CustomerID);
}
function removeCheckedCustomer(CustomerID) {
    var i = findCheckedCustomer(CustomerID);
    if (i == -1) return;
    checkedCustomer.splice(i, 1);
}
function f_isChecked(rowdata) {
    if (findCheckedCustomer(rowdata.CustomerID) == -1)
        return false;
    return true;
}
function f_onCheckRow(checked, data) {
    if (checked) addCheckedCustomer(data.ID);
    else removeCheckedCustomer(data.ID);
}
function f_getChecked() {
    alert(checkedCustomer.join(','));
}
